﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Configuration;

namespace SEO.EmailSearch
{
    public partial class UpdateFrm : Form
    {
        delegate void SetTextCallback(string text);
        private Thread catchThread = null;
        SEO.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\version.ini");

        public UpdateFrm()
        {
            InitializeComponent();
            skinEngine1.SkinFile = SEO.Utility.CommonHelper.GetCurrentSoftSkin();
            this.Icon = new Icon(SEO.Utility.ICONHelper.SetFormICON());
        }

        private void UpdateFrm_Load(object sender, EventArgs e)
        {
            lblNewVersion.Text = "最新版本：" + SEO.Utility.SoftUpdate.GetNewVersionForEmailSearch();
            lblLocationVersion.Text = "当前版本：" + iniFile.GetString("cloudin_email", "version", "");

            catchThread = new Thread(new ThreadStart(DownloadFile));
            catchThread.Start();
        }

        /// <summary>
        /// 下载升级文件
        /// </summary>
        private void DownloadFile()
        {
            try
            {
                string strSaveUpdateName = DateTime.Now.ToString("yyyyMMddHHmm");
                string strFilename = Application.StartupPath + "\\cloudin_email_acquisition_update_" + strSaveUpdateName + ".exe";

                string strFileURL = ConfigurationManager.AppSettings["DownSoftURL"].ToString();
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strFileURL);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
                float fTotal = (float)totalBytes / 1024;
                fTotal = fTotal / 1024;
                lblTotalSize.Text = "文件大小：" + fTotal.ToString("0.00") + "M";
                if (prob != null)
                {
                    prob.Maximum = (int)totalBytes;
                }
                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(strFilename, System.IO.FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);
                    if (prob != null)
                    {
                        prob.Value = (int)totalDownloadedByte;
                    }
                    osize = st.Read(by, 0, (int)by.Length);
                    float fPersent = (int)totalDownloadedByte / (float)totalBytes;
                    fPersent = fPersent * 100;
                    label3.Text = fPersent.ToString("0") + "%";
                }
                so.Close();
                st.Close();

                lblStatus.Text = "下载完成，正在启动更新程序...";
                System.Diagnostics.Process.Start(strFilename);
                btnCancel.Text = "完成";

                //退出当前程序
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("下载更新文件 -> " + ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //退出当前程序
            System.Diagnostics.Process.GetCurrentProcess().Kill();
            this.Close();
        }
    }
}
